package com.dz.risk.controller;

import com.dz.risk.common.ResponseVO;
import com.dz.risk.pojo.domain.ConfigEntity;
import com.dz.risk.pojo.model.PageModel;
import com.dz.risk.service.ConfigService;
import com.dz.risk.validation.Add;
import com.dz.risk.validation.Update;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Doke
 * @date 2021/7/9 14:25
 */
@Api(tags = "系统配置Api(数据字典)")
@RestController
@RequestMapping("/dz-ftsp/stest/config")
public class ConfigController {
    private final ConfigService configService;

    @Autowired
    public ConfigController(ConfigService configService) {
        this.configService = configService;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "configType", value = "配置项类别", dataType = "String"),
            @ApiImplicitParam(name = "configTypeCode", value = "配置项类别编码", dataType = "Integer"),
            @ApiImplicitParam(name = "configName", value = "配置字段名称", dataType = "String"),
            @ApiImplicitParam(name = "configName", value = "配置字段值", dataType = "String"),
            @ApiImplicitParam(name = "serialNumber", value = "排序号", dataType = "Integer")
    })
    @ApiOperation("新增配置信息")
    @PostMapping("/add")
    public ResponseVO add(@RequestBody @Validated({Add.class}) ConfigEntity configEntity) {
        configService.save(configEntity);
        return ResponseVO.ok();
    }

    @ApiOperation("根据ID删除配置信息")
    @ApiImplicitParam(name = "id", value = "违约债券名单Id", required = true, dataType = "String")
    @GetMapping("/deleteById")
    public ResponseVO deleteById(@RequestParam("id") String id) {
        configService.removeById(id);
        return ResponseVO.ok();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer"),
            @ApiImplicitParam(name = "configType", value = "配置项类别", dataType = "String"),
            @ApiImplicitParam(name = "configTypeCode", value = "配置项类别编码", dataType = "Integer"),
            @ApiImplicitParam(name = "configName", value = "配置项字段名称", dataType = "String"),
            @ApiImplicitParam(name = "configName", value = "配置项字段值", dataType = "String"),
            @ApiImplicitParam(name = "serialNumber", value = "排序号", dataType = "Integer")
    })
    @ApiOperation("根据Id更新配置信息 ")
    @PostMapping("/updateById")
    public ResponseVO updateById(@RequestBody @Validated({Update.class}) ConfigEntity configEntity) {
        configService.updateById(configEntity);
        return ResponseVO.ok();
    }

    @ApiOperation("分页查询配置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页(默认 : 1)", dataType = "Integer"),
            @ApiImplicitParam(name = "rows", value = "显示条数(默认 : 10)", dataType = "Integer")
    })
    @GetMapping("/pageList")
    public ResponseVO pageList(PageModel pageModel, ConfigEntity configEntity) {
        return ResponseVO.ok(configService.pageList(pageModel, configEntity));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页(默认 : 1)", dataType = "Integer"),
            @ApiImplicitParam(name = "rows", value = "显示条数(默认 : 10)", dataType = "Integer"),
            @ApiImplicitParam(name = "configType", value = "配置项类别", dataType = "String"),
            @ApiImplicitParam(name = "configTypeCode", value = "配置项类别编码", dataType = "Integer")
    })
    @GetMapping("/pageConfigType")
    @ApiOperation("分页查询配置类别")
    public ResponseVO pageConfigType(PageModel pageModel, String configType, Integer configTypeCode) {
        return ResponseVO.ok(configService.pageConfigType(pageModel, configType, configTypeCode));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "configType", value = "配置项类别", dataType = "String"),
            @ApiImplicitParam(name = "configTypeCode", value = "配置项类别编码", dataType = "Integer")
    })
    @ApiOperation("新增配置类别")
    @PostMapping("/addConfigType")
    public ResponseVO addConfigType(@RequestBody @Validated({ConfigEntity.AddConfigType.class}) ConfigEntity configEntity) {
        configService.addConfigType(configEntity);
        return ResponseVO.ok();
    }

    @ApiImplicitParam(name = "configType", value = "配置类别", required = true, dataType = "String")
    @ApiOperation("删除配置类别")
    @GetMapping("/deleteByConfigType")
    public ResponseVO deleteByConfigType(@RequestParam("configType") String configType) {
        configService.deleteByConfigType(configType);
        return ResponseVO.ok();
    }
}
